using System;
using System.Collections;
using System.Text;
using InfoJetSoft.Service.Util;
using InfoJetSoft.Service.Entity;

//CID 0167 //2008.12.15 //Begin
namespace InfoJetSoft.Service.EscapeRule
{
	/// <summary>
	/// Summary description for DataFlowParser.
	/// </summary>
	class RuleSetFilter
	{
        //CID 0626 //2010.07.12 //Begin
        private static char[] Separators = { ';' };
        //CID 0626 //2010.07.12 //End

        //CID 0735 //2010.12.19 //View view
        public static RuleSet Filter(Manifest manifest, View view, string match, RuleSet ruleSet)
        {
            //CID 0619 //2010.6.24 //Begin
            ArrayList escRules = new ArrayList();
			foreach( Rule rule in ruleSet.RuleList )
			{
				if( rule.Caption.StartsWith( "InfoJetSoft.Rules." ) )
				{
					escRules.Add( rule );
				}
			}
            foreach (Rule escRule in escRules)
            {
                //CID 0619 //2010.06.24 //Begin
                //Open Rule, match == null.
                if (escRule.Caption.Equals("InfoJetSoft.Rules.HtmlScript") && match == null )
                {
                    ruleSet.RuleList.Remove(escRule);
                    foreach (RuleAction ruleAction in escRule.RuleActionList)
                    {
                        if (ruleAction.RuleActionType == RuleAction.iDialogBoxMessage)
                        {
                            if (manifest.HtmlScriptList == null)
                            {
                                manifest.HtmlScriptList = new ArrayList();
                            }
                            manifest.HtmlScriptList.Add(ruleAction.DialogBoxMessage);
                        }
                    }
                }
                //CID 0619 //2010.06.24 //End
                //CID 0724 //2010.12.19 //Begin
                else if (escRule.Caption.Equals("InfoJetSoft.Rules.FormFieldsCache") && match == null)
                {
                    ruleSet.RuleList.Remove(escRule);
                    foreach (RuleAction ruleAction in escRule.RuleActionList)
                    {
                        if (ruleAction.RuleActionType == RuleAction.iDialogBoxMessage)
                        {
                            string setting = ruleAction.DialogBoxMessage.ToLower();
                            setting = setting.Replace(":", "");
                            setting = setting.Replace(";", "");
                            setting = setting.Replace("enabled", "");
                            setting = setting.Replace("true", "");
                            setting = setting.Trim();
                            if (setting.Length <= 0)
                            {
                                manifest.SetFormFieldsCacheValue(1);
                            }
                            else
                            {
                                manifest.SetFormFieldsCacheValue(-1);
                            }
                        }
                    }
                }
                //CID 0724 //2010.12.19 //End
                //CID 0626 //2010.07.12 //Begin
                else if (escRule.Caption.Equals("InfoJetSoft.Rules.CustomDataSources") && match == null)
                {
                    ruleSet.RuleList.Remove(escRule);
                    foreach (RuleAction ruleAction in escRule.RuleActionList)
                    {
                        if (ruleAction.RuleActionType == RuleAction.iDialogBoxMessage)
                        {
                            string[] dsNames = ruleAction.DialogBoxMessage.Split(Separators);
                            foreach (string dsName in dsNames)
                            {
                                string customDSName = dsName.Trim();
                                if (customDSName.Length > 0)
                                {
                                    if (manifest.CustomDSNames == null)
                                    {
                                        manifest.CustomDSNames = new ArrayList();
                                    }
                                    manifest.CustomDSNames.Add(customDSName);
                                }
                            }
                        }
                    }
                }
                //CID 0626 //2010.07.12 //End
                //CID 0825 //2011.03.06 //Begin
                else if (escRule.Caption.Equals("InfoJetSoft.Rules.BindingTest") && match == null)
                {
                    ruleSet.RuleList.Remove(escRule);
                    foreach (RuleAction ruleAction in escRule.RuleActionList)
                    {
                        if (ruleAction.RuleActionType == RuleAction.iDialogBoxMessage)
                        {
                            string value = ruleAction.DialogBoxMessage.Replace(";", "").Trim().ToLower();
                            if (value.Equals("enabled"))
                            {
                                manifest.EnableBindingTest = true;
                                break;
                            }
                        }
                    }
                }
                //CID 0825 //2011.03.06 //End
                //CID 0735 //2010.12.19 //Begin
                else if (escRule.Caption.Equals("InfoJetSoft.Rules.HtmlOnClick"))
                {
                    if (view != null)
                    {
                        ruleSet.RuleList.Remove(escRule);
                        StringBuilder onClick = new StringBuilder();
                        foreach (RuleAction ruleAction in escRule.RuleActionList)
                        {
                            if (ruleAction.RuleActionType == RuleAction.iDialogBoxMessage)
                            {
                                onClick.Append("\r\n");
                                onClick.Append(ruleAction.DialogBoxMessage);
                            }
                        }
                        if (onClick.Length > 0)
                        {
                            string nodeName = ExpressionUtil.parseTargetNodeName(match);
                            if (nodeName != null && nodeName.Length > 0)
                            {
                                HtmlOnClick htmlOnClick = new HtmlOnClick();
                                htmlOnClick.Id = match;
                                htmlOnClick.ViewId = view.Id;
                                htmlOnClick.OnClick = onClick.ToString();
                                if (manifest.HtmlOnClickList == null)
                                {
                                    manifest.HtmlOnClickList = new ArrayList();
                                }
                                manifest.HtmlOnClickList.Add(htmlOnClick);
                                break;
                            }
                        }
                    }
                }
                //CID 0735 //2010.12.19 //End
                //CID 0629 //2010.07.30 //Begin
                else if (escRule.Caption.Equals("InfoJetSoft.Rules.HtmlOnChange"))
                {
                    ruleSet.RuleList.Remove(escRule);
                    //CID 0676 //2010.10.13 //Begin
                    StringBuilder onChange = new StringBuilder();
                    //CID 0676 //2010.10.13 //End
                    foreach (RuleAction ruleAction in escRule.RuleActionList)
                    {
                        //CID 0676 //2010.10.13 //Begin
                        if (ruleAction.RuleActionType == RuleAction.iDialogBoxMessage)
                        {
                            onChange.Append("\r\n");
                            onChange.Append(ruleAction.DialogBoxMessage);
                        }
                        //CID 0676 //2010.10.13 //End
                    }
                    if (onChange.Length > 0)
                    {
                        string nodeName = ExpressionUtil.parseTargetNodeName(match);
                        if (nodeName != null && nodeName.Length > 0)
                        {
                            HtmlOnChange htmlOnChange = new HtmlOnChange();
                            htmlOnChange.NodeName = nodeName;
                            htmlOnChange.OnChange = onChange.ToString();
                            if (manifest.HtmlOnChangeList == null)
                            {
                                manifest.HtmlOnChangeList = new ArrayList();
                            }
                            manifest.HtmlOnChangeList.Add(htmlOnChange);
                            break;
                        }
                    }
                }
                //CID 0629 //2010.07.30 //End
                //CID 0644 //2010.08.26 //Begin
                else if (escRule.Caption.Equals("InfoJetSoft.Rules.RefreshDefaultValueOnOpenXml"))
                {
                    ruleSet.RuleList.Remove(escRule);
                    if (manifest.RefreshDefalutValueNodeNameList == null)
                    {
                        manifest.RefreshDefalutValueNodeNameList = new ArrayList();
                    }
                    string nodeName = ExpressionUtil.parseTargetNodeName(match);
                    manifest.RefreshDefalutValueNodeNameList.Add(nodeName);
                }
                //CID 0644 //2010.08.26 //End
                //CID 0622 //2010.07.01 //Begin
                else if (escRule.Caption.StartsWith("InfoJetSoft.Rules.ImportParameter"))
                {
                    ruleSet.RuleList.Remove(escRule);
                    foreach (RuleAction ruleAction in escRule.RuleActionList)
                    {
                        if (ruleAction.RuleActionType == RuleAction.iDialogBoxMessage)
                        {
                            string message = ruleAction.DialogBoxMessage.Trim();
                            ImportParameter importParameter = ImportParameter.parseContent(match, message);
                            if (importParameter != null)
                            {
                                if (manifest.ImportParameterList == null)
                                {
                                    manifest.ImportParameterList = new ArrayList();
                                }
                                manifest.ImportParameterList.Add(importParameter);
                            }
                        }
                    }
                }
                //CID 0622 //2010.07.01 //End
            }
            return Filter2( manifest, match, ruleSet );
            //CID 0619 //2010.6.24 //End
        }

		public static RuleSet Filter2( Manifest manifest, string match, RuleSet ruleSet )
		{
			ArrayList dfRules = new ArrayList();
			foreach( Rule rule in ruleSet.RuleList )
			{
				if( rule.Caption.Equals( "InfoJet Rule" ) )
				{
					dfRules.Add( rule );
				}
			}
			foreach( Rule dfRule in dfRules )
			{
				ruleSet.RuleList.Remove( dfRule );
				foreach( RuleAction ruleAction in dfRule.RuleActionList )
				{
					if( ruleAction.RuleActionType == RuleAction.iDialogBoxMessage )
					{
						string message = ruleAction.DialogBoxMessage.Trim();
						if( message.StartsWith( "ImportParameter" ) )
						{
							ImportParameter importParameter = ImportParameter.parse( match, message );
							if( importParameter != null )
							{
								if( manifest.ImportParameterList == null )
								{
									manifest.ImportParameterList = new ArrayList();
								}
								manifest.ImportParameterList.Add( importParameter );
							}
						}
						//CID 0235 //2009.03.03 //Begin
						if( message.StartsWith( "Hyperlink" ) )
						{
							Hyperlink hyperlink = Hyperlink.parse( message );
							if( hyperlink != null )
							{
								if( manifest.HyperlinkList == null )
								{
									manifest.HyperlinkList = new ArrayList();
								}
								manifest.HyperlinkList.Add( hyperlink );
							}
						}
						//CID 0235 //2009.03.03 //End
						//CID 0261 //2009.03.25 //Begin
						if( message.StartsWith( "ViewValidation" ) )
						{
							ViewValidation viewValidation = ViewValidation.parse( match );
							viewValidation.Execute( manifest.FormConfiguration );
						}
						//CID 0261 //2009.03.25 //End
						//CID 0262 //2009.03.26 //Begin
						if( message.StartsWith( "ViewSwitchValidation" ) )
						{
							manifest.FormConfiguration.ViewSwitchValiation = true;
						}
						//CID 0262 //2009.03.26 //End
					}
				}
			}

			if( ruleSet.RuleList.Count <= 0 )
			{
				return null;
			}
			else
			{
				return ruleSet;
			}
		}
	}
}
//CID 0167 //2008.12.15 //End
